#include "foreach.hpp"
#include "string_utils.hpp"
#include "unit.hpp"
#include "unit_category.hpp"
#include "unit_modification.hpp"
#include "wml_node.hpp"
#include "wml_utils.hpp"

namespace game {

unit_modification_ptr unit_modification::create(wml::const_node_ptr node)
{
	return unit_modification_ptr(new unit_modification(node));
}

unit_modification::unit_modification(wml::const_node_ptr node)
  : description_(node->attr("description")), applies_against_(0), attack_(wml::get_int(node, "attack")), defense_(wml::get_int(node, "defense"))
{
	std::vector<std::string> cats = util::split(node->attr("against"));
	foreach(const std::string& cat, cats) {
		applies_against_ = applies_against_ | get_unit_category(cat);
	}
}

bool unit_modification::applies(const unit& user, const unit& against) const
{
	return (applies_against_&against.categories()) != 0;
}


}
